/* eslint-disable no-restricted-syntax */
import moment from "moment";
import config from "config";

function setGiftDeclareValue(value, ctx, shouldSetForm = true) {
  // console.log("setGiftDeclareValue", value, ctx);
  const providerType1 = [];
  if (value.providerType1_1 === 1) {
    providerType1.push({
      key: "option1",
      slot: JSON.parse(value.providerType1_1ProviderJson || "[{}]"),
    });
  }
  if (value.providerType1_2 === 1) {
    providerType1.push({
      key: "option2",
      slot: JSON.parse(value.providerType1_2ProviderJson || "[{}]"),
    });
  }
  if (value.providerType1_3 === 1) {
    providerType1.push({
      key: "option3",
      slot: JSON.parse(value.providerType1_3ProviderJson || "[{}]"),
    });
  }
  if (value.providerType1_4 === 1) {
    providerType1.push({
      key: "option4",
      slot: JSON.parse(value.providerType1_4ProviderJson || "[{}]"),
    });
  }
  if (value.providerType1_5 === 1) {
    providerType1.push({
      key: "option5",
      slot: JSON.parse(value.providerType1_5ProviderJson || "[{}]"),
    });
  }

  const giftType2 = [];
  if (value.giftType2_1_1 === 1) {
    giftType2.push({ key: "option1", slot: value.giftType2_1_1Remark });
  }
  if (value.giftType2_1_2 === 1) {
    giftType2.push({ key: "option2", slot: value.giftType2_1_2Remark });
  }
  if (value.giftType2_1_3 === 1) {
    giftType2.push({ key: "option3", slot: value.giftType2_1_3Remark });
  }
  if (value.giftType2_1_4 === 1) {
    giftType2.push({ key: "option4", slot: value.giftType2_1_4Remark });
  }

  /**
   * 招待处理情况
   */
  const giftHandleType2_4 = [];
  for (const giftHandleTypeIndex of [1, 2, 3, 4, 5, 6]) {
    const key = `giftHandleType2_4_${giftHandleTypeIndex}`;
    if (value[`${key}`] === 1) {
      const result = {
        key: `option${giftHandleTypeIndex}`,
        slot: {
          fileList: JSON.parse(value[`${key}GiftApplyBillFileJson`] || "[]"),
          message: value[`${key}Remark`] || "",
        },
      };
      giftHandleType2_4.push(result);
    }
  }

  const giftProviderHaveRelatives = [];
  if (value.giftProviderHaveRelatives === 1) {
    giftProviderHaveRelatives.push({
      key: `option1`,
      slot: value.giftProviderHaveRelativesRemark,
    });
  } else if (value.giftProviderHaveRelatives === 0) {
    giftProviderHaveRelatives.push({
      key: `option2`,
    });
  }

  const giftObtainChannel = [];
  if (value.giftObtainChannel) {
    if (value.giftObtainChannel === 1) {
      giftObtainChannel.push({
        key: `option${value.giftObtainChannel}`,
        slot: {
          message: value.giftObtainChannelRemark,
          fileList: JSON.parse(
            value.giftObtainChannelGiftApplyBillFileJson || "[]"
          ),
        },
      });
    } else if (value.giftObtainChannel === 4) {
      giftObtainChannel.push({
        key: `option${value.giftObtainChannel}`,
        slot: value.giftObtainChannelRemark4,
      });
    } else {
      giftObtainChannel.push({
        key: `option${value.giftObtainChannel}`,
      });
    }
  }

  const giftProviderHaveProjects = [];
  if (value.giftProviderHaveProjects) {
    const optionKey = `option${value.giftProviderHaveProjects}`;
    const remarkKey = `giftProviderHaveProjectsRemark${value.giftProviderHaveProjects === 1 ? '' : value.giftProviderHaveProjects}`;
    giftProviderHaveProjects.push({
      key: optionKey,
      slot: value[remarkKey],
    });
  }

  console.log("giftHandleType2_4", value)
  const settedValue = {
    providerType1,
    giftType2,
    giftHandleType2_4,
    giftPrice: value.giftPrice,
    giftDate: value.giftDate ? moment(value.giftDate) : undefined,
    singleProviderGiftDescription2_5_1:
      value.singleProviderGiftDescription2_5_1,
    singleProviderGiftDescription2_5_2:
      value.singleProviderGiftDescription2_5_2,
    giftProviderHaveRelatives,
    giftObtainChannel,
    giftProviderHaveProjects,
  };

  if (config.appEnv === "dev") {
    console.log("settedValue", settedValue);
  }

  if (ctx.props && ctx.props.form && shouldSetForm) {
    ctx.props.form.setFieldsValue({
      "gift-declare": settedValue,
    });
  }

  return settedValue;
}

function getGiftDeclareValue(formData, ctx) {
  const values = ctx.props.form.getFieldsValue();
  const {
    providerType1 = [],
    giftType2 = [],
    giftHandleType2_4 = [],
    giftPrice,
    giftDate,
    singleProviderGiftDescription2_5_1,
    singleProviderGiftDescription2_5_2,
    giftProviderHaveRelatives = [],
    giftObtainChannel = [],
    giftProviderHaveProjects = [],
  } = values["gift-declare"] || {};

  let {
    businessCode,
    companyOID,
    applicantName,
    applicantOID,
    departmentOID,
    departmentName,
  } = formData;

  try {
    if (!applicantName) {
      applicantName = ctx.state.info.applicant.fullName;
    }
    if (!applicantName) {
      applicantName = ctx.props.user.fullName;
    }
  } catch (e) {
    applicantName = ctx.props.user.fullName;
  }

  const result = {
    businessCode,
    companyOID,
    applicantName,
    applicantOID,
    departmentOID,
    departmentName,
    providerType1_1: 0,
    providerType1_1ProviderJson: "[]",
    providerType1_2: 0,
    providerType1_2ProviderJson: "[]",
    providerType1_3: 0,
    providerType1_3ProviderJson: "[]",
    providerType1_4: 0,
    providerType1_4ProviderJson: "[]",
    providerType1_5: 0,
    providerType1_5ProviderJson: "[]",
    giftType2_1_1: 0,
    giftType2_1_1Remark: "",
    giftType2_1_2: 0,
    giftType2_1_2Remark: "",
    giftType2_1_3: 0,
    giftType2_1_3Remark: "",
    giftType2_1_4: 0,
    giftType2_1_4Remark: "",
    giftDate: giftDate
      ? moment(giftDate).format("YYYY-MM-DD HH:mm:SS")
      : undefined,
    giftPrice,
    giftHandleType2_4_1: 0,
    giftHandleType2_4_1GiftApplyBillFileJson: "[]",
    giftHandleType2_4_1Remark: "",
    giftHandleType2_4_2: 0,
    giftHandleType2_4_2GiftApplyBillFileJson: "[]",
    giftHandleType2_4_2Remark: "",
    giftHandleType2_4_3: 0,
    giftHandleType2_4_3GiftApplyBillFileJson: "[]",
    giftHandleType2_4_3Remark: "",
    giftHandleType2_4_4: 0,
    giftHandleType2_4_4Remark: "",
    giftHandleType2_4_4GiftApplyBillFileJson: "[]",
    giftHandleType2_4_5: 0,
    giftHandleType2_4_5Remark: "",
    giftHandleType2_4_5GiftApplyBillFileJson: "[]",
    giftHandleType2_4_6: 0,
    giftHandleType2_4_6Remark: "",
    giftHandleType2_4_6GiftApplyBillFileJson: "[]",
    singleProviderGiftDescription2_5_1: singleProviderGiftDescription2_5_1,
    singleProviderGiftDescription2_5_2: singleProviderGiftDescription2_5_2,
    // 礼品提供方是否和自己具有亲属关系 0：否 1：是
    giftProviderHaveRelatives: undefined,
    giftProviderHaveRelativesRemark: "",
    giftProviderHaveRelativesRemark2: "",
    // 礼品获取渠道 1：招待/招商会等 2：邮件/快递 3：当面交付 4：其他
    giftObtainChannel: undefined,
    giftObtainChannelGiftApplyBillFileJson: "[]",
    giftObtainChannelGiftApplyBillFileJson2: "[]",
    giftObtainChannelGiftApplyBillFileJson3: "[]",
    giftObtainChannelGiftApplyBillFileJson4: "[]",
    giftObtainChannelRemark: "",
    giftObtainChannelRemark2: "",
    giftObtainChannelRemark3: "",
    giftObtainChannelRemark4: "",
    // 礼品提供方是否有和公司正在进行的项目 0：没有 1：有 2：不清楚
    giftProviderHaveProjects: undefined,
    giftProviderHaveProjectsRemark: "",
    giftProviderHaveProjectsRemark2: "",
    giftProviderHaveProjectsRemark3: "",
    giftProviderHaveProjectsRemark4: "",
  };

  // 招待提供方信息
  if (providerType1) {
    for (const option of providerType1) {
      const index = option.key.substr(6);
      result[`providerType1_${index}`] = 1;
      result[`providerType1_${index}ProviderJson`] = JSON.stringify(
        option.slot
      );
    }
  }

  // 招待类型
  if (giftType2) {
    for (const option of giftType2) {
      const index = option.key.substr(6);
      result[`giftType2_1_${index}`] = 1;
      result[`giftType2_1_${index}Remark`] = option.slot;
    }
  }

  // 招待处理情况
  if (giftHandleType2_4) {
    console.log("giftHandleType2_4", giftHandleType2_4);
    for (const option of giftHandleType2_4) {
      const index = option.key.substr(6);
      result[`giftHandleType2_4_${index}`] = 1;
      if (option.slot) {
        result[`giftHandleType2_4_${index}GiftApplyBillFileJson`] =
          JSON.stringify(option.slot.fileList);
        result[`giftHandleType2_4_${index}Remark`] = option.slot.message;
      }
    }
  }

  // 招待的提供方是否为亲属关系 单选
  if (giftProviderHaveRelatives !== undefined) {
    for (const option of giftProviderHaveRelatives) {
      const index = option.key && option.key.substr(6);
      result.giftProviderHaveRelatives = index === "1" ? 1 : 0;
      result[`giftProviderHaveRelativesRemark${index === "1" ? "" : index}`] =
        option.slot || "";
    }
  }

  // 招待获取的渠道 单选
  if (giftObtainChannel) {
    for (const option of giftObtainChannel) {
      const index = option.key.substr(6);
      result.giftObtainChannel = Number(index);
      if (index === "1") {
        if (option.slot) {
          result[`giftObtainChannelRemark${index === "1" ? "" : index}`] =
            option.slot.message;
          result[
            `giftObtainChannelGiftApplyBillFileJson${
              index === "1" ? "" : index
            }`
          ] = JSON.stringify(option.slot.fileList);
        }
      }
      if (index === "4") {
        result[`giftObtainChannelRemark${index === "1" ? "" : index}`] =
          option.slot || "";
      }
    }
  }

  if (giftProviderHaveProjects) {
    for (const option of giftProviderHaveProjects) {
      const index = option.key && option.key.substr(6);
      console.log("1111", option);
      result.giftProviderHaveProjects = Number(index);
      result[
        `giftProviderHaveProjectsRemark${
          Number(index) === 1 ? "" : Number(index)
        }`
      ] = option.slot || "";
    }
  }

  console.log("edited value", result);
  return result;
}

export { setGiftDeclareValue, getGiftDeclareValue };
